﻿using AlOss;
using System;

namespace Activity.Helper
{
    /// <summary>
    /// 图片压缩处理
    /// </summary>
    public static class ImageHelper
    {
        /// <summary>
        /// 获取图片URL
        /// </summary>
        /// <param name="bucketName"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string GetOssUrl(string bucketName, string url)
        {
            if (string.IsNullOrWhiteSpace(bucketName))
                return url;
            return OssHelper.GetImageUrl(bucketName, url);
        }

        /// <summary>
        /// 保存图片
        /// </summary>
        /// <param name="bucketName"></param>
        /// <param name="toSave"></param>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static bool SavaOss(string bucketName, string toSave, byte[] bytes)
        {
            if (string.IsNullOrWhiteSpace(bucketName))
                return false;
            var oss = OssHelper.GetClient(bucketName);
            oss.UploadData(toSave, bytes);
            return true;
        }

        /// <summary>
        /// 保存图片
        /// </summary>
        /// <param name="bucketName"></param>
        /// <param name="toSave"></param>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static IAsyncResult UploadDataAsync(string bucketName, string toSave, byte[] bytes)
        {
            var oss = OssHelper.GetClient(bucketName);
            return oss.UploadDataAsync(toSave, bytes);
        }

        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="bucketName"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public static bool DeleteOssImg(string bucketName, string url)
        {
            if (string.IsNullOrWhiteSpace(bucketName))
                return false;
            OssHelper.GetClient(bucketName).DeleteObject(url);
            return true;
        }
    }

}